/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.saleticket.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.BdTag;
import com.lightkits.common.db.entity.WmSaleTicketLine;
import com.lightkits.common.db.entity.event.BdEvent;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.utility.date.DateUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.saleticket.CreateChangeParams;
import com.lightkits.wms.domain.saleticket.model.SaleConstrant;
import com.lightkits.wms.dto.onhand.model.OnhandDTO;
import com.lightkits.wms.dto.saleticket.model.UpSaleTicketHeadDto;
import com.lightkits.wms.dto.saleticket.model.UpSaleTicketLineDto;
import com.lightkits.wms.dto.stocktransfer.model.SubStockViewDTO;
import com.lightkits.wms.dto.tagservice.model.CancelTagDto;
import com.lightkits.wms.dto.tagservice.model.TagViewDto;
import com.lightkits.wms.util.models.Constrants;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jing.wei@hand-china.com
 * @date 2019/7/1 10:29
 **/
@Service
public class CreateChangeParamsImpl implements CreateChangeParams {
    /**
     * 创建现有量变更参数
     *
     * @param headDto   售货单头参数
     * @param eventId   事件Id(作为现有量变更得父事件)
     * @param requestId 请求
     * @param lineDtos  售货单行参数
     * @return
     */
    @Override
    public List<OnhandDTO> createOnhandParams(UpSaleTicketHeadDto headDto,
                                              Long eventId,
                                              Long requestId,
                                              List<UpSaleTicketLineDto> lineDtos) {
        List<OnhandDTO> onhandDTOS = new ArrayList<>();
        try {
            //根据售货单行状态判断是库存变更还是子库存转移
            for (UpSaleTicketLineDto dto : lineDtos) {
                if (dto.getLineStatus().equals(SaleConstrant.SALE_CANCEL)) {
                    continue;
                }
                OnhandDTO params = new OnhandDTO();
                params.setWorkerId(headDto.getWorkerId());
                params.setParentEventId(eventId.toString());
                params.setEventRequestId(requestId);
                params.setOrgId(headDto.getOrgId());
                params.setItemId(dto.getItemId());
                params.setUomCode(dto.getUomCode());
                params.setOwnerType(Constrants.OWN);
                params.setOwnerId(dto.getOwnerId());
                params.setLotNumber(dto.getLotNum());
                params.setChangeQuantity(dto.getQuantity());
                params.setEventTypeCode(SaleConstrant.MOVE_TRANS_OUT);
                params.setWarehouseId(dto.getWarehouseId());
                params.setWmAreaId(dto.getWmAreaId());
                params.setChangeDirection(Constrants.OUT);
                onhandDTOS.add(params);
            }
        } catch (Exception e) {
            throw new LightKitsBizException(e);
        }
        return onhandDTOS;
    }

    /**
     * 创建子库存转移参数
     *
     * @param headDTO   售货单头参数
     * @param eventId   事件Id(作为现有量变更得父事件)
     * @param requestId 请求
     * @param lineDTOS  售货单行参数
     * @return List<SubStockViewDTO>
     */
    @Override
    public List<SubStockViewDTO> createSubStockParam(UpSaleTicketHeadDto headDTO,
                                                     Long eventId,
                                                     Long requestId,
                                                     List<UpSaleTicketLineDto> lineDTOS) {
        List<SubStockViewDTO> subStockViewDTOS = new ArrayList<>();
        try {
            for (UpSaleTicketLineDto line : lineDTOS) {
                if (!line.getLineStatus().equals(SaleConstrant.SALE_CANCEL)) {
                    continue;
                }
                SubStockViewDTO viewDTO = new SubStockViewDTO();
                viewDTO.setWorkerId(headDTO.getWorkerId());
                viewDTO.setOrgId(headDTO.getOrgId());
                viewDTO.setItemId(line.getItemId());
                viewDTO.setEventRequestId(requestId.toString());
                viewDTO.setUomCode(line.getUomCode());
                viewDTO.setLot(line.getLotNum());
                viewDTO.setParentEventId(eventId.toString());
                viewDTO.setOutWarehouseId(line.getWarehouseId());
                viewDTO.setOutWmAreaId(line.getWmAreaId());
                viewDTO.setInWarehouseId(line.getToWarehouseId());
                viewDTO.setOwnerType(Constrants.OWN);
                viewDTO.setOwnerId(line.getOwnerId());
                viewDTO.setTransferQty(line.getQuantity());
                viewDTO.setMoveTypeCode(SaleConstrant.MOVE_TRANS_ALLOT);
                subStockViewDTOS.add(viewDTO);
            }
        } catch (Exception e) {
            throw new LightKitsBizException(e);
        }
        return subStockViewDTOS;
    }

    /**
     * 创建取消状态条码信息
     *
     * @param lines 扫描订单行
     * @param orgId 组织ID(转移的)
     * @return List<TagViewDto>
     */
    @Override
    public List<TagViewDto> createTags(List<UpSaleTicketLineDto> lines, BdEvent event, String orgId) {
        List<TagViewDto> tagViewDTOS = new ArrayList<>();
        try {
            for (UpSaleTicketLineDto dto : lines) {
                if (!dto.getLineStatus().equals(SaleConstrant.SALE_CANCEL)) {
                    continue;
                }
                TagViewDto viewDTO = new TagViewDto();
                viewDTO.setTagCode(dto.getTagCode());
                BdTag tag = new BdTag().selectOne(new EntityWrapper().eq(BdTag.TAG_CODE, dto.getTagCode()));
                viewDTO.setTagId(tag.getId().toString());
                viewDTO.setTagType(tag.getTagType());
                viewDTO.setDocumentId(dto.getTicketLineId());
                viewDTO.setOwnerOrgId(orgId);
                viewDTO.setEventId(event.getId());
                viewDTO.setEventBy(event.getWorkerId());
                viewDTO.setEventTime(DateUtil.toDateString(event.getCreationDate()));
                viewDTO.setVerificationCode(tag.getVerificationCode());
                viewDTO.setDCode(tag.getDCode());
                viewDTO.setProductCode(tag.getProductCode());
                viewDTO.setWarehouseId(dto.getToWarehouseId());
                viewDTO.setStorageUnitCode(tag.getStorageUnitCode());
                viewDTO.setTagStatus(tag.getTagStatus());
                viewDTO.setContainerOwnerType(tag.getContainerOwnerType());
                viewDTO.setContainerType(tag.getContainerType());
                viewDTO.setRemark(tag.getRemark());
                if (!ObjectUtil.isEmpty(tag.getCapacityQty())) {
                    viewDTO.setCapacityQty(tag.getCapacityQty().stripTrailingZeros().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getOuterTagId())) {
                    viewDTO.setOuterTagId(tag.getOuterTagId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getResourceId())) {
                    viewDTO.setResourceId(tag.getResourceId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getLocationId())) {
                    viewDTO.setLocationId(tag.getLocationId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getContainerId())) {
                    viewDTO.setContainerId(tag.getContainerId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getDocumentTypeId())) {
                    viewDTO.setDocumentTypeId(tag.getDocumentTypeId().toString());
                }
                viewDTO.setItemId(dto.getItemId());
                viewDTO.setTagThingType("ITEM");
                viewDTO.setUomCode(dto.getUomCode());
                viewDTO.setPrimaryUomCode(dto.getUomCode());
                if (!ObjectUtil.isEmpty(dto.getQuantity())) {
                    viewDTO.setQuantity(dto.getQuantity());
                    viewDTO.setPrimaryQuantity(dto.getQuantity());
                }
                viewDTO.setLotNumber(dto.getLotNum());
                viewDTO.setOwnerType(Constrants.OWN);
                if (!ObjectUtil.isEmpty(dto.getOwnerId())) {
                    viewDTO.setOwnerId(dto.getOwnerId());
                }
                tagViewDTOS.add(viewDTO);
            }

        } catch (Exception e) {
            throw new LightKitsBizException(e);
        }
        return tagViewDTOS;
    }

    /**
     * @param lineDtos 售货单行
     * @param event    物料
     * @return List<CancelTagDto>
     */
    @Override
    public List<CancelTagDto> createCancelTag(List<UpSaleTicketLineDto> lineDtos, BdEvent event) {
        List<CancelTagDto> cancelTagDtos = new ArrayList<>();
        try {
            lineDtos.forEach(target -> {
                CancelTagDto tagDto = new CancelTagDto();
                tagDto.setTagId(target.getTagId());
                tagDto.setTagCode(target.getTagCode());
                tagDto.setEventId(event.getId());
                tagDto.setEventBy(event.getWorkerId());
                tagDto.setEventTime(DateUtil.toDateString(event.getCreationDate()));
                cancelTagDtos.add(tagDto);
            });
        } catch (Exception e) {
            throw new LightKitsBizException(e);
        }
        return cancelTagDtos;
    }

    /**
     * @param lineDtos 售货单行
     * @param event    物料
     * @return List<CancelTagDto>
     */
    @Override
    public List<CancelTagDto> createSimCancelTag(List<WmSaleTicketLine> lineDtos, BdEvent event) {
        List<CancelTagDto> cancelTagDtos = new ArrayList<>();
        try {
            lineDtos.forEach(target -> {
                CancelTagDto tagDto = new CancelTagDto();
                tagDto.setTagId(target.getTagId().toString());
                BdTag tag = new BdTag().selectById(target.getTagId());
                tagDto.setTagCode(tag.getTagCode());
                tagDto.setEventId(event.getId());
                tagDto.setEventBy(event.getWorkerId());
                tagDto.setEventTime(DateUtil.toDateString(event.getCreationDate()));
                cancelTagDtos.add(tagDto);
            });
        } catch (Exception e) {
            throw new LightKitsBizException(e);
        }
        return cancelTagDtos;
    }
}
